import pymysql

# 创建连接对象
conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='',
    database='test'
)
# 获取游标对象
cursor = conn.cursor()

# 防止sql注入
try:
    sql = "insert into user (name, age) values (%s, %s)"
    row_count = cursor.execute(sql, ['sn', 22])
    print("影响行数: {}".format(row_count))
    conn.commit()
except Exception as e:
    print(e)
    conn.rollback()


# 关闭游标
cursor.close()
# 关闭连接1.test1.py
conn.close()

